package com.jy.appmarket.dialog;

import com.jy.appmarket.R;
import com.jy.appmarket.utils.UITools;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;


public class EscRecommendDialog extends Dialog {

	private TextView mTitle;
	private ImageView mImage;
	private Button mCancel;
	private Button mDownload;
	private long mLastClickBackTime;
	public boolean exitFlag = false;
	public EscRecommendDialog(Context context) {
		super(context, R.style.custom_dialog_theme);
		initView();
	}
	
	private void initView(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.esc_recommend_dialog_layout);
		mTitle = findViewById(R.id.esc_recommend_dialog_title_tv);
		mImage = findViewById(R.id.esc_recommend_dialog_icon_iv);
		mCancel = findViewById(R.id.esc_recommend_dialog_buttonleft);
		mDownload = findViewById(R.id.esc_recommend_dialog_buttonright);
	}
	
	public EscRecommendDialog showTitle(String title){
		if(!TextUtils.isEmpty(title)){
			mTitle.setText(title);
		}
		return this;
	}
	
	public EscRecommendDialog showIcon(Bitmap icon, long lastclickbacktime, android.view.View.OnClickListener listener){
		mLastClickBackTime = lastclickbacktime;
		if(icon != null){
			LinearLayout.LayoutParams params = (LayoutParams) mImage.getLayoutParams();
			double w = UITools.getDeviceScreenWidthHeight(getContext())[0]*0.8;
			params.width = (int) w;
			params.height = (int) (w*icon.getHeight()/icon.getWidth());
			mImage.setLayoutParams(params);
			mImage.setImageBitmap(icon);
			params = (LayoutParams) mCancel.getLayoutParams();
			int bw = params.width;
			params.leftMargin = (int)(w-bw*2)/3;
			params.rightMargin = (int)(w-bw*2)/6;
			mCancel.setLayoutParams(params);
			
			params = (LayoutParams) mDownload.getLayoutParams();
			params.rightMargin = (int)(w-bw*2)/3;
			params.leftMargin = (int)(w-bw*2)/6;
			mDownload.setLayoutParams(params);
			
		}
		if(listener != null){
			mImage.setOnClickListener(listener);
		}
		return this;
	}
	
	public EscRecommendDialog showButtonLeft(String text, android.view.View.OnClickListener listener){
		if(!TextUtils.isEmpty(text)){
			mCancel.setText(text);
		}
		if(listener != null){
			mCancel.setOnClickListener(listener);
		}
		return this;
	}
	
	public EscRecommendDialog showButtonRight(String text, android.view.View.OnClickListener listener){
		if(!TextUtils.isEmpty(text)){
			mDownload.setText(text);
		}
		if(listener != null){
			mDownload.setOnClickListener(listener);
		}
		return this;
	}	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		long now = System.currentTimeMillis();
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(Math.abs(now - mLastClickBackTime) < 2000){
				exitFlag = true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
